//DSA ASSIGNMENT 1
//ANKUR GOEL
//BTECH 2010
//ROLL NUMBER - 2010016
//Implementation of list attribute of python in c language using arrays


#include<stdio.h>
#include<stdlib.h>


void showlist(int a[])
	{
		printf("\n\n The list is : ");
		int count=0;
		printf(" [ ");
		while(a[count+1]!=-1)
			{	
				printf("%d , " , a[count]);
				count++;
			}
		printf("%d ]\n\n" , a[count]);
	}


void createlist(int a[])
	{
		int count=0;
		int yesno;

		for( ; count<10000 ; count++)
				a[count]=-1;

		count=0;
		do
		 {
			printf("\n\n Enter element number %d of your new list : " , count+1);
			scanf("%d" , &a[count]);
			count++;
			showlist(a);
			printf("\n Do you want to enter more elements (1--> yes , 2--> no ) : ");
			scanf("%d" , &yesno);		

			while((yesno!=1)&&(yesno!=2))
				{
					printf("\n\n You entered an incorrect choice , please enter again !! \n");
					printf("\n Do you want to enter more elements (1--> yes , 2--> no ): ");
					scanf("%d" , &yesno);		
				}		

		 }while(yesno == 1);

	}		


void append(int a[])
	{	
		int count=0,x,yesno=2;
		while(yesno==2)
			{		
				printf("\n\n Enter the element that you wish to append in the list : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to append ( 1--> yes , 2--> no , i`ll enter element again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the list that you wish to append ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf("%d" , &yesno);	
					}

			}
		while(a[count+1]!=-1)
		count++;
		a[count+1]=x;
		printf("\n\n SUCCESS !!! Element has been added at end of list \n");
		showlist(a);
	}	

	


void extend(int a[])
	{
		int yesno=2;		
		int list2[10000];
		while(yesno==2)
			{
				createlist(list2);	
				showlist(list2);
				printf("\n Is this the list that you wish to extend into your list ( 1--> yes , 2--> no , i`ll enter list again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! ");
						showlist(list2);
						printf("\n Is this the list that you wish to extend into your list ( 1--> yes , 2--> no , i`ll enter list again ) : ");
						scanf("%d" , &yesno);	
					}

			}
		int count1=0 , count2=0;
		while(a[count1]!=-1)
			count1++;
		while(list2[count2]!=-1)
		      {			
			a[count1]=list2[count2];
			count1++;
			count2++;
		      }
		printf("\n\n SUCCESS !!! Your list has been extended. \n");
		showlist(a);
	}			



void insert(int a[])
	{
		int pos , temp=0 ,store, x , yesno=2;

		while(yesno==2)
			{		
				printf("\n\n Enter the element that you wish to append in the list : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to append ( 1--> yes , 2--> no , i`ll enter element again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the element that you wish to append ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf("%d" , &yesno);	
					}

			}

		yesno=2;
		while(yesno==2)
			{		
				printf("\n\n Enter the position at which you wish to append in the list : ");
				scanf("%d" , &pos);
				printf("\n The position you entered is : %d" , pos);
				printf("\n Is this the position that you wish to append at ( 1--> yes , 2--> no , i`ll enter position again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The position you entered is : %d" , pos);
						printf("\n Is this the position that you wish to append at ( 1--> yes , 2--> no , i`ll enter position again ) : ");
						scanf("%d" , &yesno);	
					}	
 
			}

		
		if(a[pos-1]==-1)
			{
				printf("\n\n FAILED !!! Your element %d hasnt been inserted in the list at position entered as the position doesnt exist in the list.\n" , x );
				showlist(a);
			}
		else
			{
				while(a[temp]!=-1)
					temp++;

				while(temp>pos-1)
				   {
					store=a[temp-1];
					a[temp-1]=a[temp];
					a[temp]=store;
					temp--;
				   }
	
				a[pos-1]=x;
				printf("\n\n SUCCESS !!! Your element %d has been inserted in the list at position %d. \n" , x , pos);
				showlist(a);
			}
	}


void removal(int a[])
	{
		int temp=0 ,flag=0,store, x , yesno;
		
		showlist(a);
		do
			{		
				printf("\n\n Enter the element that you wish to remove from the list : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to remove ( 1--> yes , 2--> no , i`ll enter element again ) : ");
				scanf(" %d" , &yesno);	
				while((yesno != 1)&&(yesno != 2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the element that you wish to remove ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf(" %d" , &yesno);	
					}
			}while(yesno==2);

		while(a[temp]!=-1)
			{		
				if(a[temp]==x)
		 			{	
						flag=1;
						while(a[temp+1]!=-1)
		   				{
							store=a[temp+1];
							a[temp+1]=a[temp];
							a[temp]=store;
							temp++;
			        		}
						a[temp]=-1;
						printf("\n\n SUCCESS !!! Your element %d has been removed from the list at its first occurence . \n" , x );
						showlist(a);
						break;
					}
				temp++;
			}

		if(flag==0)
			{
				printf("\n\n FAILED !!! Your element %d has not been found in the list . \n" , x );
				showlist(a);
			}
	
	}
			
		

int pop(int a[])
	{
		int yesno=2 , pos , store , popreturn=-1;
		showlist(a);
		while(yesno==2)
			{		
				printf("\n\n Enter the position at which you wish to pop the element in the list : ");
				scanf("%d" , &pos);
				printf("\n The position you entered is : %d" , pos);
				printf("\n Is this the position that you wish to pop at ( 1--> yes , 2--> no , i`ll enter position again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{	
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The position you entered is : %d" , pos);
						printf("\n Is this the position that you wish to pop at ( 1--> yes , 2--> no , i`ll enter position again ) : ");
						scanf("%d" , &yesno);	
					}

			}
		pos--;

		if(a[pos]==-1)
			{
				printf("\n\n FAILED !!! The list isnt large enough , there is no element at the entered position.\n ");
				showlist(a);
			}

		else
			{
	
				while(a[pos+1]!=-1)
	   				{
						store=a[pos+1];
						a[pos+1]=a[pos];
						a[pos]=store;
						pos++;
        				}
				popreturn=a[pos];			
				a[pos]=-1;
				printf("\n\n SUCCESS !!! The element popped at the entered position is : %d \n" , popreturn );
				showlist(a);
			}

		return popreturn;
	}



int indexs(int a[])
	{
		int yesno , indexreturn=-1 , temp=0 , x;
		showlist(a);
		
		do
			{		
				printf("\n\n Enter the element that you wish to find the first occurence of : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to find the first occurence of : ( 1--> yes , 2--> no , i`ll enter element again ) : ");
				scanf(" %d" , &yesno);	
				while((yesno != 1)&&(yesno != 2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the element that you wish to find the first occurence of : ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf(" %d" , &yesno);	
					}

			}while(yesno==2);

		while(a[temp]!=-1)
			{		
				if(a[temp]==x)
		 			{	
						indexreturn=temp+1;
						break;
					}
				temp++;
			}

		if(indexreturn==-1)
			printf("\n\n FAILED !!! The entered element was not found in the list.\n ");

		else
			printf("\n\n SUCCESS !!! The first occurence of the entered element %d was found at the position : %d \n" , x , indexreturn);
		showlist(a);	
		return indexreturn;

	}
								

int count(int a[])
	{
		int yesno , countreturn=0 , temp=0 , x;
		showlist(a);
		
		do
			{		
				printf("\n\n Enter the element that you wish to find the times of occurence of : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to find the times of occurence of : ( 1--> yes , 2--> no , i`ll enter element again ) : ");
				scanf(" %d" , &yesno);	
				while((yesno != 1)&&(yesno != 2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the element that you wish to find the times of occurence of : ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf(" %d" , &yesno);	
					}

			}while(yesno==2);

		
		while(a[temp]!=-1)
			{		
				if(a[temp]==x)
		 			{	
						countreturn++;
					}
				temp++;
			}
	
		if(countreturn==0)
			{
				printf("\n\n FAILED !!! The entered element was not found in the list.\n ");
				countreturn=-1;
			}
				
		else
			printf("\n\n SUCCESS !!! The times of occurence of the entered element %d in the list is : %d \n" , x , countreturn);

		showlist(a);	
		return countreturn;

	}


void sort(int a[])
	{
		int temp=0 , small , pos , store ;
		showlist(a);
		while(a[temp]!=-1)
			{
				small=a[temp];
				int j=temp+1;
				while(a[j]!=-1)
					{
						if(a[j]<small)
						   {	
							small=a[j];
							pos=j;
						   }	
						j++;		
					}
				store=a[temp];
				a[temp]=small;
				a[pos]=store;
				temp++;
			}
		printf("\n\n SUCCESS !!! The list has been sorted. \n");
		showlist(a);				
	}


void reverse(int a[])
	{
		int list2[10000] , count=0,temp=0;
		showlist(a);
		while(count<10000) 
		    {	
			list2[count]=-1;
			count++;
		    }
		count=0;			
		while(a[temp+1]!=-1)
			temp++;
		while(temp>=0)
			{
				list2[count]=a[temp];
				temp--;
				count++;
			}
		count=0;
		temp=0;
		while(list2[count]!=-1)
			{
				a[temp]=list2[count];
				temp++;
				count++;
			}
		printf("\n\n SUCCESS !!! The list has been reversed. \n");
		showlist(a);				
	}



int listsize(int a[])
	{
		int elementcount=0;
		showlist(a);
		while(a[elementcount]!=-1)
			elementcount++;

		printf("\n\n The number of elements in the list is : %d \n\n" , elementcount);

		if(elementcount==0)
			elementcount=-1;
		return elementcount;
	}
		

void main()
{
	int list1[10000];
	int popreturn=-1;
	int indexreturn=-1 , countreturn=-1 , elementcount=-1;
	int choice=0,flag=0;
	while (choice!=12)
		{
			if(flag==0)
				{
				  flag=1;
			  	  createlist(list1);
				  showlist(list1);
				
				}
	
			printf("\n    	       MAIN MENU !!!     \n");
			printf("\n	 What do you want to do ??? \n");
			printf("\n	 1.  APPEND  --- Append  an item at the end of your list.");
			printf("\n	 2.  EXTEND  --- Extend the list by appending all the items in a list at the end.");
			printf("\n	 3.  INSERT  --- Add an element in the list at a particular position.");
			printf("\n	 4.  REMOVE  --- Remove the first occurence of an element from your list.");
			printf("\n 	 5.  POP     --- Pop the item at a given position in your list and return it.");
			printf("\n	 6.  INDEX   --- Return the index of the first occurence of an item in your list.");
			printf("\n	 7.  COUNT   --- Return the number of times an element appears in the list");
			printf("\n	 8.  SORT    --- Sort the elements of the list.");
			printf("\n	 9.  REVERSE --- Reverse the elements of your list.");
			printf("\n	 10. SHOW    --- Displays the list.");
			printf("\n	 11. SIZE    --- Returns the total number of elements in the list.");
			printf("\n	 12. EXIT    --- Exit the program.");
			printf("\n\n     Enter the function number of your choice : ");
			scanf("%d" , &choice);

			int checkswitch=0;
			while(checkswitch==0)
			{
			   switch(choice)
				{
					case 1 : append(list1);
						 checkswitch=1;
						 break;

					case 2 : extend(list1);
						 checkswitch=1;
						 break;

					case 3 : insert(list1);
						 checkswitch=1;
						 break;

					case 4 : removal(list1);
						 checkswitch=1;
						 break;

					case 5 : popreturn=pop(list1);
						 checkswitch=1;
						 break;

					case 6 : indexreturn=indexs(list1);
						 checkswitch=1;
						 break;

					case 7 : countreturn=count(list1);
						 checkswitch=1;
						 break;

					case 8 : sort(list1);
						 checkswitch=1;
						 break;

					case 9 : reverse(list1);
						 checkswitch=1;
						 break;

					case 10 : showlist(list1);
						  checkswitch=1;
						  break;

					case 11 : elementcount=listsize(list1);
						  checkswitch=1;
						  break;

					case 12 : printf("\n\n Thank you for using this program.   \n\n ");
						  checkswitch=1;
						  break;

					default :
						  printf("\n\n The choice you entered wasnt valid !! \n Please choose again !!\n");
						  choice=0;
						  while((choice<1)||(choice>10))
							{
					 			printf("\n\n     Enter the function number of your choice : ");
								scanf("%d" , &choice);
							}
				
				}
		
			}

		}

}
